import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QPushButton

class BMRWindow(QWidget):
    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):

        genderLabel = QLabel('性别：', self)
        genderLabel.move(76, 20)

        self.genderLineEdit = QLineEdit(self)
        self.genderLineEdit.move(120, 20)

        weightLabel = QLabel('体重（kg）：', self)
        weightLabel.move(30, 60)

        self.weightLineEdit = QLineEdit(self)
        self.weightLineEdit.move(120, 60)

        heightLabel = QLabel('身高（cm）：', self)
        heightLabel.move(30, 100)

        self.heightLineEdit = QLineEdit(self)
        self.heightLineEdit.move(120, 100)

        ageLabel = QLabel('年龄（岁）：', self)
        ageLabel.move(30, 140)

        self.ageLineEdit = QLineEdit(self)
        self.ageLineEdit.move(120, 140)

        self.resultLabel = QLabel('计算结果', self)
        self.resultLabel.move(30, 225)
        self.resultLabel.setFixedHeight(20)
        self.resultLabel.setFixedWidth(500)

        calcButton = QPushButton('计算', self)
        calcButton.move(200, 180)
        calcButton.clicked.connect(self.calcBMR)

        self.setGeometry(900, 300, 500, 300)
        self.setWindowTitle('BMR计算器')
        self.show()

    def calcBMR(self):
        gender = self.genderLineEdit.text().lower()
        weight = self.weightLineEdit.text()
        height = self.heightLineEdit.text()
        age = self.ageLineEdit.text()

        try:
            weight = float(weight)
            height = float(height)
            age = int(age)
        except ValueError as e:
            self.resultLabel.setText("体重、身高和年龄目前暂不支持输入空值或其他表示，请输入数字。")
            return

        if gender == "男":
            bmr = 66 + (13.7 * weight) + (5 * height) - (6.8 * age)
            self.resultLabel.setText("您的BMR为：{:.2f}。".format(bmr))
        elif gender == "女":
            bmr = 655 + (9.6 * weight) + (1.8 * height) - (4.7 * age)
            self.resultLabel.setText("您的BMR为：{:.2f}。".format(bmr))
        else:
            self.resultLabel.setText("抱歉，目前国服只有男性和女性可供选择，更多选择请期待新版本。")
            return

if __name__ == '__main__':
    app = QApplication(sys.argv)
    bmrWindow = BMRWindow()
    sys.exit(app.exec_())